// Direct3D_Class project
// Jonathan S. Harbour
// June 2005

#include <d3d9.h>
#include <d3dx9.h>
#include "CSurface.h"


CSurface::CSurface(LPDIRECT3DDEVICE9 dev)
{
	SetDevice(dev);
}

CSurface::~CSurface()
{
	surface->Release();
}

int CSurface::Load(char *filename, D3DCOLOR transcolor)
{
    D3DXIMAGE_INFO info;
    HRESULT result;
    
    //get width and height from bitmap file
    result = D3DXGetImageInfoFromFile(filename, &info);
    if (result != D3D_OK)
	{
        surface = NULL;
		return 1;
	}

    //create surface
    result = device->CreateOffscreenPlainSurface(
        info.Width,         //width of the surface
        info.Height,        //height of the surface
        D3DFMT_X8R8G8B8,    //surface format
        D3DPOOL_DEFAULT,    //memory pool to use
        &surface,           //pointer to the surface
        NULL);              //reserved (always NULL)

    if (result != D3D_OK)
	{
        surface = NULL;
		return 1;
	}

    //load surface from file into newly created surface
    result = D3DXLoadSurfaceFromFile(
        surface,            //destination surface
        NULL,               //destination palette
        NULL,               //destination rectangle
        filename,           //source filename
        NULL,               //source rectangle
        D3DX_DEFAULT,       //controls how image is filtered
        transcolor,         //for transparency (0 for none)
        NULL);              //source image info (usually NULL)

    //make sure file was loaded okay
    if (result != D3D_OK)
	{
        surface = NULL;
		return 1;
	}

	return 0;
}


